<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
use think\facade\Session;
use think\facade\Request;
use app\common\model\Admin;
use app\common\model\Article;

class Index extends AdminBase
{
/*
	public function initialize()
	{
		if(!Session::has('user_id')){
			$this->error('请登录','index/user/login');
		}
	}
*/
	//管理员登录
	public function login()
	{
		$this->isLogged();
		if(Request::isAjax()){
			$data = Request::post();
			//实例化验证器
			$validata = new \app\common\validate\Admin();
			if (!$validata->scene('login')->check($data)) {
				return $this->error( $validata->getError());
			}
			$user = Admin::where(['username'=>$data['username'],'password'=>$data['password']])->find();
			//halt($user);
			if($user){
				//将用户数据写入Session
				Session::set('admin_id',$user['id']);
				Session::set('admin_name',$user['username']);
				$user->save(
                        [
                            'last_login_time' => time(),
                            'last_login_ip' => $this->request->ip(),
                        ]
                    );
				return $this->success('登陆成功','admin/index/index');
             //return json(array('code' => 200, 'msg' => '登录成功'));
			}else{
				return json(['code' => 0, 'msg' => '用户名或密码错误']);
			}
		}
		return view();
	}


    public function index()
	{
		$this->isLogin();
		$user['user_id'] = session::get('user_id');
		$username = session::get('user_name');
		//halt($user['user_id']);
		$article = Article::where('user_id',$user['user_id'])->select();
		$this->assign('article',$article);
        return view();
    }
	
	
	
	public function login2()
	{
		return view();
	}

    public function set()
	{
        return view();
    }

    public function message(){
        return view();
    }

    public function home(){
        return view();
    }
	
	
	  public function layout(){
		
        return view();
    }
}